#include "gamefield.h"

GameField::GameField(QWidget *parent) :
    QWidget(parent),
    owner(0),
    isSelected(false),
    isSquare(false)
{
    setMouseTracking(true);
    setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    QPalette palette;
    palette.setBrush(QPalette::Background,QBrush(Qt::lightGray));
    setPalette(palette);
}

void GameField::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    painter.setRenderHint(QPainter::Antialiasing);

    if (getisSquare() && owner)
    {
        painter.setBrush(owner->color.darker());
    }
    else
    {
        painter.setBrush(palette().background());
    }
    painter.save();
    painter.scale(width(),height());
    painter.drawRect(0,0,1,1);
    painter.restore();

    //painter.drawText(0,0,width(),height(),Qt::AlignCenter | Qt::AlignHCenter, QString::number(index.x())+" "+QString::number(index.y()) );
}

void GameField::enterEvent(QEvent *)
{
    if (!isSelected && !owner)
    {
        QPalette palette=this->palette();
        palette.setBrush(QPalette::Background,QBrush(palette.background().color().darker(200)));
        setPalette(palette);
    }
}

void GameField::leaveEvent(QEvent *)
{
    if (palette().background().color()!=Qt::lightGray && !isSelected && !owner)
    {
        QPalette palette=this->palette();
        palette.setBrush(QPalette::Background,QBrush(palette.background().color().lighter(200)));
        setPalette(palette);
    }
}

void GameField::setIsSelected(bool value)
{
    QPalette palette=this->palette();
    isSelected = value;
    if( isSelected )
    {
        if (!owner)
        {
            palette.setBrush(QPalette::Background,QBrush(Qt::green));
        }
        else
        {
            palette.setBrush(QPalette::Background,QBrush(owner->color.darker()));
        }
    }
    else
    {
        if (!owner)
        {
            palette.setBrush(QPalette::Background,QBrush(Qt::lightGray));
        }
        else
        {
            palette.setBrush(QPalette::Background,QBrush(owner->color));
        }
    }
    setPalette(palette);
}

bool GameField::getIsSelected() const
{
    return isSelected;
}
